﻿/*  
 * Helix Engine
 * http://helixengine.codeplex.com/
 * 
 * Copyright (c) 2010 - 2011 Joe Hegarty
 *  
 * This source code is subject to the terms of the Microsoft Public License (Ms-PL). 
 *  
 * Redistribution and use in source and binary forms, with or without modification, 
 * is permitted provided that redistributions of the source code retain the above 
 * copyright notices and this file header. 
 * 
 * For details please see http://www.opensource.org/licenses/ms-pl.html 
 *  
 * All other rights reserved. 
 *
 */  


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

using HelixEngine.Maths;

namespace HelixEngine.Input
{
    /// <summary>
    /// 
    /// </summary>
    public class InputManager
    {
        bool[] _keysDown = new bool[255];
        bool _mouseDown = false;
        bool _mouseClick = false;
        Vector2 _mousePoint;
        Image _target;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="root"></param>
        public InputManager(Image root)
        {
            _target = root;

            _target.KeyDown += new KeyEventHandler(OnKeyDown);
            _target.KeyUp += new KeyEventHandler(OnKeyUp);
            _target.MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseDown);
            _target.MouseLeftButtonUp += new MouseButtonEventHandler(OnMouseUp);
            _target.MouseMove += new MouseEventHandler(OnMouseMove);
            _target.MouseEnter += new MouseEventHandler(OnMouseEnter);
            _target.MouseLeave += new MouseEventHandler(OnMouseLeave);

            ClearState();
        }

        /// <summary>
        /// 
        /// </summary>
        public void ClearState()
        {
            for (int i = 0; i < 255; i++)
            {
                _keysDown[i] = false;
            }
            _mouseDown = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="k"></param>
        /// <returns></returns>
        public bool IsKeyDown(Key k)
        {
            int v = (int)k;
            if (v < 0 || v > 82) return false;
            return _keysDown[v];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool IsMouseDown()
        {
            return _mouseDown;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool IsMouseClick()
        {
            return _mouseDown && _mouseClick;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Vector2 GetMousePoint()
        {
            return _mousePoint;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Update()
        {
            _mouseClick = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnKeyDown(Object sender, KeyEventArgs e)
        {
            int index = (int)e.Key;
            if (index < _keysDown.Length)
            {
                _keysDown[index] = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnKeyUp(Object sender, KeyEventArgs e)
        {
            int index = (int)e.Key;
            if (index < _keysDown.Length)
            {
                _keysDown[index] = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMouseDown(Object sender, MouseButtonEventArgs e)
        {
            _mouseDown = true;
            _mouseClick = true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMouseUp(Object sender, MouseButtonEventArgs e)
        {
            _mouseDown = false;
            _mouseClick = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMouseMove(Object sender, MouseEventArgs e)
        {
            Point point = e.GetPosition(null);
            Vector2 mousePoint = new Vector2();
            mousePoint.X = point.X;
            mousePoint.Y = point.Y;
            _mousePoint = mousePoint;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMouseEnter(Object sender, MouseEventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMouseLeave(Object sender, MouseEventArgs e)
        {

        }
    }
}
